package cn.chenfyuan.demoapplication.codegen;

import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.stereotype.Component;

/***
 * 代码生成
 * @author linweijian
 * @Date 2018/5/2 19:53
 **/
@Component
public class CodeGenerator {

    private CodeGeneratorConfig generatorConfig;
    public CodeGenerator(CodeGeneratorConfig generatorConfig) {
        this.generatorConfig = generatorConfig;
    }

    /**
     * 代码生成
     */
    public void generateCode() {
        String packageName = generatorConfig.getPackageName();
        String tables=generatorConfig.getTables();
        String[] tableNames =tables.split(",");
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName, tableNames);
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = generatorConfig.getDbUrl();
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setTypeConvert(new MySqlTypeConvert() {
                    // 自定义数据库表字段类型转换【可选】
                    @Override
                    public DbColumnType processTypeConvert(String fieldType) {
                        System.out.println("转换类型：" + fieldType);
                        // 注意！！processTypeConvert 存在默认类型转换，如果不是你要的效果请自定义返回、非如下直接返回。
                        return super.processTypeConvert(fieldType);
                    }
                })
                .setUrl(dbUrl)
                .setUsername(generatorConfig.getDbUserName())
                .setPassword(generatorConfig.getDbPassword())
                .setDriverName(generatorConfig.getDbDriverName());
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                //.setCapitalMode(true)
                //.setEntityLombokModel(false)
                // .setEntityBuilderModel(false)
                //.setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setRestControllerStyle(true)
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        config.setActiveRecord(false)
                .setAuthor(generatorConfig.getAuthor())
                .setOutputDir(generatorConfig.getPath())
                .setFileOverride(true)
                .setEnableCache(false)//二级缓存
                .setBaseResultMap(true)
                .setBaseColumnList(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }

}
